﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Globalization;
public partial class Main_Reports_Inventory_ROL_Report : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    DataTable DT = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
        }
    }
    protected void GetData()
    {
        if (txtfromdate.Text != "" && txttodate.Text != "")
        {
            SqlParameter[] ParamROL={ new SqlParameter("@fromdate",Convert.ToDateTime(txtfromdate.Text).ToString(string.Format("yyyy/MM/dd"))),
                                        new SqlParameter("@todate",Convert.ToDateTime(txttodate.Text).ToString(string.Format("yyyy/MM/dd")))
                                    };
            DataTable DT=DU.getdatausingprocedure("IVR_RPT_ROL_REPORT",ParamROL);
            if(DT.Rows.Count>0)
            {
                grdVIewROL.DataSource = DT;
                grdVIewROL.DataBind();
                lblRecordNo.Text = grdVIewROL.Rows.Count.ToString() + " Record(s)";
                ddlColumnname.Style["display"] = "block";
                lblvalue.Style["display"] = "block";
                txtValue.Style["display"] = "block";
                imgbtnPrint.Style["display"] = "block";
                ImgBtnExcel.Style["display"] = "block";            
            }
        }
    }
    protected void grdVIewROL_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdVIewROL.PageIndex = e.NewPageIndex;
        GetData();
    }
    protected void btngetreport_Click(object sender, EventArgs e)
    {
        GetData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        grdVIewROL.Visible = true;
        GetData();
        string Filter = "";
        if (ddlColumnname.SelectedValue == "1")//For BinNo
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "BinRef Like'%" + txtValue.Text + "%'");
            grdVIewROL.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "2")//for Item Code
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Itemcode like'%" + txtValue.Text + "%'");
            grdVIewROL.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "3")//For AltItemCode
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "AltItemCode Like'%" + txtValue.Text + "%'");
            grdVIewROL.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "4")//for MPN
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "MPN Like'%" + txtValue.Text + "%'");
            grdVIewROL.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "5")//For ROL
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ROL Like'%" + txtValue.Text + "%'");
            grdVIewROL.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "6")//Item Name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Itemname='" + txtValue.Text + "'");
            grdVIewROL.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "7") //For the Project
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Project='" + txtValue.Text + "'");
            grdVIewROL.AllowPaging = false;
        }
        else
        {
            GetData();
        }
        DataRow[] Dr = DT.Select(Filter);
        DataTable Tempdt = new DataTable();
        Tempdt = DT.Clone();
        foreach (DataRow drNew in Dr)
        {
            Tempdt.ImportRow(drNew);
        }
        grdVIewROL.DataSource = Tempdt;
        grdVIewROL.DataBind();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "CriticalItem_Report.xls"));
            Response.ContentType = "application/ms-excel";
            grdVIewROL.ShowFooter = false;
            grdVIewROL.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Response.Write(sw.ToString());
            grdVIewROL.RenderControl(htw);
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
}